Exercises
Alright, let's get some hands-on practice with useEffect
!
Logging a particular value
Below, we have a signup form with several React state variables.
Our goal is to add a console.log
that fires only when the value of “email” changes. We should see the user's email logged in the console whenever they edit that field.
Acceptance Criteria:
- Whenever the user changes the value of the “email” state variable, the new value should be logged to the console.
- Nothing should be logged when the user changes another field (for example, their city or postal code).
- The logging should be done inside an effect, not within the
onChange
event handler.
Stretch Goal:
- Update the code so that
name
is also logged whenever it changes.
Code Playground
Solution:
Locally-persisted state
Let's suppose we're building an application with a “Dark Mode” toggle.
It would be really annoying if users had to keep toggling their preferred mode every time they load our application!
Let's update the code below so that the user's preference is saved in localStorage, and restored when the page loads.
In other words, the current value of isDarkMode
should be "remembered", and used when the page is refreshed:
We can do this in plain JS with the following code:
// Save the value:window.localStorage.setItem('is-dark-mode', true);
// Retrieve the value:window.localStorage.getItem('is-dark-mode');
Acceptance Criteria:
- The value of
isDarkMode
should be saved in localStorage whenever it changes, using theuseEffect
hook. - The initial value of the
isDarkMode
state variable should be retrieved from localStorage (or set tofalse
if no value has been saved). - You can use the string
"is-dark-mode"
for the key. - Note: Items saved to localStorage are always saved as a string. You'll need to convert the stored value back to boolean. You can do this with
JSON.parse()
.
Code Playground
Solution:
In this video, we use a function to initialize our state variable. This pattern is discussed in the “useState Hook” lesson.
One more thing: the solution shared here won't work if using a server-side rendering framework like Next.js, Remix, or Gatsby. We'll learn more about this scenario and how to work around it later in this course.